package springboot.study.project.util.file;


import springboot.study.project.common.TableVerifyInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

public class FileUtil {

    // 日志打印
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * @Author: liu-hao
     * @Description: 将list按行写入到txt文件中（每次覆盖）
     * @Date: 2020/10/24 18:31
     * @Param: [dataList, path]
     * @return: void
     **/
    public void writeFileContext(List<Map<String, String>> dataList, String path) throws Exception {
        File file = new File(path);
        //如果没有文件就创建
        if (!file.isFile()) {
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));

        writer.write("totalCount--->" + dataList.size() + "\r\n");
        for (Map<String, String> record : dataList) {
            for (Map.Entry<String, String> entity : record.entrySet()) {
                String colInfo = entity.getKey() + "--->" + entity.getValue();
                writer.write(colInfo + "   ");
            }
            writer.write("\r\n");
        }
        writer.flush();
        writer.close();
    }


    /**
     * @Author: liu-hao
     * @Description: 将字符串追加到文件中去
     * @Date: 2020/10/24 18:31
     * @Param: [dataList, path]
     * @return: void
     **/
    public static void writeStringToFile(String remark, String content, String path) throws IOException {
        File file = new File(path);
        //如果没有文件就创建
        if (!file.isFile()) {
            file.createNewFile();
        }
        // true,则追加写入text文本
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));

        writer.write(remark + "--->" + content + "\r\n");
        writer.write("\r\n");

        writer.flush();
        writer.close();
    }

    /**
     * @Author: liu-hao
     * @Description: 将核准数据导出到Excel表中
     * @Date: 2020/10/26 16:37
     * @Param: [tableVerifyInfoList, path]
     * @return: boolean
     **/
    public static boolean writeExeclFileContext(List<TableVerifyInfo> tableVerifyInfoList, String path) {
        boolean result = false;

        //第一步，创建一个workbook对应一个excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第二部，在workbook中创建一个sheet对应excel中的sheet
        HSSFSheet sheet = workbook.createSheet("数据核对");
        //第三部，在sheet表中添加表头第0行，老版本的poi对sheet的行列有限制
        HSSFRow row = sheet.createRow(0);
        //第四步，创建单元格，设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("总表数");
        cell = row.createCell(1);
        cell.setCellValue("表名");
        cell = row.createCell(2);
        cell.setCellValue("表注释");
        cell = row.createCell(3);
        cell.setCellValue("表字段数据量");
        cell = row.createCell(4);
        cell.setCellValue("表数据量");

        //第五步，写入实体数据，实际应用中这些数据从数据库得到,对象封装数据，集合包对象。对象的属性值对应表的每行的值
        for (int i = 0; i < tableVerifyInfoList.size(); i++) {
            HSSFRow row1 = sheet.createRow(i + 1);
            TableVerifyInfo tableVerifyInfo = tableVerifyInfoList.get(i);
            //创建单元格设值
            row1.createCell(0).setCellValue(tableVerifyInfo.getTableCount());
            row1.createCell(1).setCellValue(tableVerifyInfo.getTableName());
            row1.createCell(2).setCellValue(tableVerifyInfo.getTableComment());
            row1.createCell(3).setCellValue(tableVerifyInfo.getTableFieldCount());
            row1.createCell(4).setCellValue(tableVerifyInfo.getTableRecordCount());
        }

        //将文件保存到指定的位置
        try {
            FileOutputStream fos = new FileOutputStream(path);
            workbook.write(fos);
            result = true;
            logger.info(path + "写入成功");
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
